// mine/index/index.js
import Toast from '../../ui/toast/index'
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
const app = getApp();
var pic_icon={
    icon1:app.base_url+"wximage/icon/dfk_icon.png",
    icon1:app.base_url+"wximage/icon/dfk_icon.png",
    icon1:app.base_url+"wximage/icon/dfk_icon.png",
    icon1:app.base_url+"wximage/icon/dfk_icon.png",
    icon1:app.base_url+"wximage/icon/dfk_icon.png",
    icon1:app.base_url+"wximage/icon/dfk_icon.png",
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        no_user_pic:defaultAvatarUrl,
        pic_url:app.base_url,
        mine_active:true,
        icon:{
            icon1:app.base_url+"wximage/icon/dfk_icon.png",
            icon2:app.base_url+"wximage/icon/dsh_icon.png",
            icon3:app.base_url+"wximage/icon/pj_icon.png",
            icon4:app.base_url+"wximage/icon/sh_icon.png",
            icon5:app.base_url+"wximage/icon/dd_icon.png",
            icon6:app.base_url+"wximage/icon/yhq_icon.png",
            icon7:app.base_url+"wximage/icon/kf_icon.png",
            icon8:app.base_url+"wximage/icon/sc_icon.png",
            icon9:app.base_url+"wximage/icon/sz_icon.png",
        },
        is_login:false,
        user:{},
        car_goods_num:0,
        notice:[],
    },

    //会员登录
    to_login(){
        wx.login({
            success:(res)=>{
                if(res.code){
                    app.http.request("index.php?r=api/user/login",{code:res.code}).then((res)=>{
                        if(res.data.result){
                            wx.setStorageSync('token', res.data.data);
                            Toast({
                                context:this,
                                message:"登录成功",
                                selector:"#toast"
                            });
                            this.setData({
                                is_login:true,
                            });
                            this.get_user_info();
                        }else{
                            wx.showModal({
                              title: '提示',
                              content: res.data.data,
                              showCancel:false,
                              complete: (res) => {
                                if (res.confirm) {
                                  
                                }
                              }
                            })
                        }
                    });
                }else{
                    Toast({
                        context:this,
                        message:"登录失败",
                        selector:"#toast"
                    });
                }
            },
            fail:()=>{}
        });
        
    },

    //获取用户信息
    get_user_info(){
        app.login.request("index.php?r=api/user/info").then((res)=>{
            if(res.data.result){
                this.setData({
                    is_login:true,
                    user:res.data.data,
                });
                this.get_pay_log();
            }else{
                Toast({
                    context:this,
                    selector:"#toast",
                    message:res.data.data,
                });
            }
        });
    },

     //更新购物车数量
     refresh_car(){
        let goods = wx.getStorageSync('goods');
        let num=0;
        if(goods){
            num=goods.length;
        }
        this.setData({
            car_goods_num:num,
        });
    },

    //跳转订单列表
    to_order(){
        wx.navigateTo({
          url: '/mine/order/list/list',
        })
    },

    //获取最近10条消费记录滚动
    get_pay_log(){
        app.login.request("index.php?r=api/user/paylog").then((res)=>{
            console.log(res);
            if(res.data.result){
                this.setData({
                    notice:res.data.data,
                });
            }
        });
    },

    //跳转余额
    to_paynum(){
        wx.navigateTo({
          url: '/mine/paynum/index',
        })
    },

    //跳转优惠券
    to_coupon(){
        wx.navigateTo({
          url: '/mine/coupon/index',
        })
    },

    //跳转vip
    to_vip(){
        wx.navigateTo({
          url: '/mine/vip/index',
        })
    },

    //跳转首页
    to_home(){
        wx.redirectTo({
            url: '/pages/index/index',
        })
    },

    //跳转分类
    to_cate(){
        wx.redirectTo({
            url: '/cate/index/index',
        })
    },

    //跳转购物车
    to_car(){
        wx.redirectTo({
            url: '/car/index/index',
        })
    },

    //显示客服
    show_kf(){
        wx.showLoading({
          title: '加载中',
        })
    },

    //隐藏客服
    hide_kf(){
        wx.hideLoading();
    },

    //修改信息
    to_info(){
        wx.navigateTo({
          url: '/mine/info/index',
        })
    },

    //退出登录
    user_out(){
        wx.showModal({
          title: '提示',
          content: '确定要退出登录么',
          complete: (res) => {
            if (res.cancel) {
              
            }
        
            if (res.confirm) {
              wx.setStorageSync('token', "");
              this.setData({
                  is_login:false,
              });
            }
          }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        app.get_name();
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.refresh_car();
        this.hide_kf();
        this.get_user_info();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})